//
//  GYLoginViewController.m
//  Visitor
//
//  Created by x on 2017/9/11.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYLoginViewController.h"
#import "GYLoginView.h"
#import "GYPwdRetrievalViewController.h"
#import "GYConfigureLoginViewController.h"
#import "GYRegisterViewController.h"
#import "GYNetWorkTools.h"


@interface GYLoginViewController ()<GYLoginViewDelegate,CAAnimationDelegate>

/** 登入view */
@property (nonatomic, weak) GYLoginView *loginView;
@end

@implementation GYLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //1.初始化
    [self setupUI];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"登入";
    
    //1.添加右侧按钮
    //self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithBack:@"enter_icon_set_nor" HightImage:@"enter_icon_set_pre" Target:self action:@selector(setInfomation)];
    
    //2.登入界面
    GYLoginView *loginView = [[GYLoginView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
    loginView.delegate = self;
    self.loginView = loginView;
    [self.view addSubview:loginView];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //3.监听键盘frame的改变
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ketBoardFrameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
/** 键盘弹出事件 */
- (void)ketBoardFrameChanged:(NSNotification *)noti {
    CGRect keyBoardRect = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationTime = [[noti.object objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];
    //代表键盘弹出
    [UIView animateWithDuration:animationTime animations:^{
        if (keyBoardRect.origin.y < HEIGHT) {
            self.loginView.center = CGPointMake(WIDTH/2, (HEIGHT-64)/2-60);
        }else {
            self.loginView.center = CGPointMake(WIDTH/2, (HEIGHT-64)/2);
        }
    }];
}

- (void)setInfomation {
    GYConfigureLoginViewController *configureLoginVC = [[GYConfigureLoginViewController alloc] initWithNibName:@"GYConfigureLoginViewController" bundle:nil];
    [self.navigationController pushViewController:configureLoginVC animated:YES];
}

#pragma mark -- <GYLoginViewDelegate>

- (void)loginBtuClick:(NSString *)userName pwd:(NSString *)pwd {
    [SVProgressHUD showWithStatus:@"登入中..."];
    //15346218437   156464
    [GYNetWorkTools loginWithUser:userName PassWord:pwd Success:^(NSDictionary *obj) {
        //NSLog(@"login======%@",obj);
        GYUserInfo *userInfo = [[GYUserInfo alloc] initWithDic:obj];
        //2.跳转界面VC
        if (userInfo.status == 200) {
            //1.保存用户信息
            [userInfo saveUserInfo];
            
            //0.把deviceToken上传给服务器
            [GYNetWorkTools uploadDeviceToken:[[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"] requestKey:[[GYUserInfo loadUserInfo] requestKey] susscess:^(NSDictionary *obj) {
                //NSLog(@"object==========%@",obj);
                if ([[obj objectForKey:@"status"] intValue] == 200) {
                    NSLog(@"deciceTokenUploadSuccess......");
                }
            } filed:^(NSError *error) {
                NSLog(@"deciceTokenUploadFailed======%@",error);
            }];
            
            [SVProgressHUD dismiss];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"changeRootController" object:nil];
        }else {
            [SVProgressHUD showErrorWithStatus:@"用户名/密码错误"];
        }
    } Filed:^(NSError *error) {
        NSLog(@"loginFiled======%@",error);
        [SVProgressHUD showErrorWithStatus:@"登入失败,请检查网络,重新登入!"];
    }];
}

- (void)registerBtuClick {
    
    GYRegisterViewController *registerController = [[GYRegisterViewController alloc] initWithNibName:@"GYRegisterViewController" bundle:nil];
    [self.navigationController pushViewController:registerController animated:YES];
}

- (void)pwdRetrieval {
    GYPwdRetrievalViewController *pwdRetrievalVC = [[GYPwdRetrievalViewController alloc] initWithNibName:@"GYPwdRetrievalViewController" bundle:nil];
    [self.navigationController pushViewController:pwdRetrievalVC animated:YES];
}


@end
